package com.bdqn.shiro.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bdqn.shiro.config.shiro.MyShiroRealm;
import com.bdqn.shiro.pojo.Right;
import com.bdqn.shiro.pojo.Role;
import com.bdqn.shiro.pojo.SysRight;
import com.bdqn.shiro.service.RightService;
import com.bdqn.shiro.service.RoleService;
import com.bdqn.shiro.service.SysRightService;
import com.bdqn.shiro.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.mgt.RealmSecurityManager;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class RoleController {

    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private SysRightService sysRightService;
    @Resource
    private RightService rightService;

    @RequestMapping("role/list")
    public String List(Model model, Role role, @RequestParam(defaultValue = "1") Long pageIndex) {
        Page<Role> page = new Page<>();
        page.setCurrent(pageIndex);
        page.setRecords(roleService.getList(page, role));
        model.addAttribute("rolePager", page);
        model.addAttribute("roleName", role.getRoleName());
        model.addAttribute("pageIndex", pageIndex);
        return "role/list";
    }

    @GetMapping("role/add")
    public String toAdd(Model model) {
        model.addAttribute("rights", sysRightService.findRightByRoleId(null));
        return "role/add";
    }

    @PostMapping("role/save")
    public String save(Role role, String[] rightCodes) {
        // 如果角色的启用状态为null则设置为0
        if (role != null && role.getRoleFlag() == null) {
            role.setRoleFlag(0);
        }
        // 使用Service层方法实现添加角色及对应的权限关系数据
        boolean save = roleService.saveRoleAndRight(role, rightCodes);
        if (save) {
            // 清空权限缓存
            RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
            MyShiroRealm realm = (MyShiroRealm) rsm.getRealms().iterator().next();
            realm.clearAllCachedAuthorizationInfo();
            return "redirect:/role/list";
        }
        return "role/add";
    }

    @ResponseBody
    @GetMapping("role/del/{roleId}")
    public String del(Model model, @PathVariable(value = "roleId") Long roleId) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("usr_role_id", roleId);
        Long row = userService.count(queryWrapper);
        Map map=new HashMap();
        if (row>0){
            map.put("result","inuse");
        }else{
            QueryWrapper que=new QueryWrapper();
            que.eq("rf_role_id",roleId);
            rightService.remove(que);
            roleService.removeById(roleId);
            map.put("result","true");
        }
        // 清空权限缓存
        RealmSecurityManager rsm = (RealmSecurityManager) SecurityUtils.getSecurityManager();
        MyShiroRealm realm = (MyShiroRealm) rsm.getRealms().iterator().next();
        realm.clearAllCachedAuthorizationInfo();
        return JSON.toJSONString(map);
    }

    @GetMapping("role/edit")
    public String edit(Model model,Role role){
        Role roleList=roleService.getById(role.getRoleId());
        roleList.setRights(sysRightService.findRightByRoleId(role.getRoleId()));
        model.addAttribute("role",roleList);
        model.addAttribute("rights",sysRightService.findRightByRoleId(null));
        return "role/edit";
    }
}
